package pe.com.hermes.hermetic.negocio.ultil;

import java.io.File;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.engine.xml.JRXmlWriter;


class JasperToXml {
    public static String sourcePath, destinationPath, xml, outputPath;
    public static JasperDesign jd = new JasperDesign();

    public static void main(String[] args) {

        try {
             String directorioJasper = "C:\\temp\\hermetic\\jasper\\";            
             String JASPER_EXT = ".jasper";
             String JRXML_EXT = ".jrxml";              
             int tamanoJasperExt =  JASPER_EXT.length();             
             File directorio = new File(directorioJasper);
             
             if (directorio!= null && directorio.isDirectory()){
                String[] nombresJasper = directorio.list();
             
                for(int idx=0;idx<nombresJasper.length;idx++){
                    String nombreJasperExt = nombresJasper[idx];                    
                    boolean isJasper = nombreJasperExt.endsWith(JASPER_EXT);
                    
                    if (nombreJasperExt!= null && isJasper){                    
                        String nombreFile = nombreJasperExt.substring(0,nombreJasperExt.length() - tamanoJasperExt);
                        
                        sourcePath = directorioJasper + nombreFile+JASPER_EXT;
                        outputPath = directorioJasper + nombreFile+JRXML_EXT;
    
                        JasperReport report = (JasperReport)JRLoader.loadObject(sourcePath);                        
                        JRXmlWriter.writeReport(report, outputPath, "UTF-8");
                        
                    }else{
                        System.out.println("Nombre en blanco o no es extesion "+ JASPER_EXT);
                    }
                    
                }
             
             }else{
                System.out.println("La ruta indicada no es un directorio");                
             }
                
            /*String fileJasper = "HMR2005";
            sourcePath = "C:\\temp\\hermetic\\jasper\\"+ fileJasper+".jasper";
            outputPath = "C:\\temp\\hermetic\\jasper\\"+ fileJasper+".jrxml";

            JasperReport report = 
                (JasperReport)JRLoader.loadObject(sourcePath);
            JRXmlWriter.writeReport(report, outputPath, "UTF-8");
            */
            
            /*sourcePath = "..File1.jasper";
            outputPath = "...File2.jrxml";*/
            
            // for converting a JasperReport object (compiled .jasper file) to a JasperDesign object
            //jd  = JRXmlLoader.load(sourcePath);
            // is this the right way to do it???
        } catch (JRException e) {
            e.printStackTrace();
        }finally{
            System.out.println("Termino el proceso Jasper to jrxml");
        }
        //JRXmlWriter.writeReport(jd, outputPath); // 


    }
}

